package com.fy.controller;

import com.fy.dao.Forum_infoMapper;
import com.fy.dao.User_infoMapper;
import com.fy.entity.Forum_info;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/foruminfo")
public class Forum_infoController {
    String id = "1";
    @Resource
    Forum_infoMapper forum_infoMapper;

    @Resource
    User_infoMapper userInfoMapper;
    @GetMapping("/getAll")
    public List<Forum_info> forum_infoMapper() {
        return forum_infoMapper.findAll();
    }

    @PostMapping("/getInfo")
    public List<Forum_info> getinfo(String for_id) {
        List<Forum_info> co = forum_infoMapper.getInfo(for_id);
        return co;
    }

    @PostMapping("/insert")
    public String insert(String for_id, String info_id, String for_type, String for_title, String for_info, String for_pic,
                         int for_like, int for_collection, String for_comments) {
        forum_infoMapper.insert_forum_info(id, info_id, for_type, for_title, for_info, for_pic, for_like, for_collection, for_comments);
        id=(Integer.valueOf(id)+1+"");
        userInfoMapper.update_info(null,null,null,null,2,info_id);

        return "200";
    }

    @PostMapping("/delete")
    public String delete(String for_id) {
        forum_infoMapper.delete_forum_info(for_id);
        return "200";
    }

    @PostMapping("/update")
    public String update(int for_like, int for_collection, String for_id) {
        List<Forum_info> all = forum_infoMapper.findAll();
        for (Forum_info info : all) {
            if (for_collection == 0) {
                if (info.getFor_id().equals(for_id)) {
                    for_like = info.getFor_like() + 1;
                    for_collection = info.getFor_collection();
                }
            }
            if (for_like == 0) {
                if (info.getFor_id().equals(for_id)) {
                    for_collection = info.getFor_collection() + 1;
                    for_like = info.getFor_like();
                }
            }
            if (for_collection == -1) {
                if (info.getFor_id().equals(for_id)) {
                    for_like = info.getFor_like() - 1;
                    for_collection = info.getFor_collection();
                }
            }
            if (for_like == -1) {
                if (info.getFor_id().equals(for_id)) {
                    for_collection = info.getFor_collection() - 1;
                    for_like = info.getFor_like();
                }
            }

        }
        forum_infoMapper.update_forum_info(for_like, for_collection, for_id);
        return "200";
    }
}
